import java.rmi.RemoteException;

import javax.faces.FactoryFinder;
import javax.faces.application.Application;
import javax.faces.application.ApplicationFactory;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.event.ActionEvent;
import javax.xml.rpc.ServiceException;

import uvprocess.UserService;
import uvprocess.UserServiceBindingStub;
import uvprocess.UserServiceLocator;

import com.sun.corba.se.pept.transport.ContactInfo;
import com.sun.jmx.remote.util.Service;

public class UVBean {
	
	private String operation;
	private String name;
	private String description;
    private Integer CM;
    private Integer TP;
    private Integer TD;
    private Integer BE;
    private Integer controle;
    private Integer id;
    private String status;
    private Integer year;
    
    public String addUV(){
    	operation = "Add";
    	return "ok";
    }

    public void action(ActionEvent event){
		System.out.println("Reading");
        this.operation = (String)event.getComponent().
                getAttributes().get("operation");
    }
    
    public String save(){
//    	FacesContext context = FacesContext.getCurrentInstance();
//    	String operation = context.getExternalContext().getRequestParameterMap().get("operation").toString();
    	String res= "-1";
    	if (operation.equals("Add")){
    		try {
    			FacesContext facesContext = FacesContext.getCurrentInstance();
    			ValueBinding valueBinding = facesContext.getApplication().createValueBinding("#{userbean}");
    			Userbean user = (Userbean) valueBinding.getValue(facesContext);
				UserServiceLocator ws = new UserServiceLocator();
				res = ws.getUserServicePort().addUV(name, description, year, CM, TD, TP, BE, controle, user.getUsername(), user.getUsername(), user.getPassword());
	    		System.out.println("Adding uv " + res);
			} catch (Exception e) {
	    		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage( FacesMessage.SEVERITY_INFO, "Something went wrong", "Something went wrong"));
	    		return "fail";
			}
    	} else {
    		System.out.println("Updating uv");
    	}
    	if (!res.equals("-1")){
        	return "ok";
    	} else {
    		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage( FacesMessage.SEVERITY_INFO, "Could not add UV", "Could not add UV"));
    		return "fail";
    	}
    }
    
	public String getOperation() {
		return operation;
	}
	public void setOperation(String operation) {
		this.operation = operation;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Integer getCM() {
		return CM;
	}
	public void setCM(Integer cm) {
		CM = cm;
	}
	public Integer getTP() {
		return TP;
	}
	public void setTP(Integer tp) {
		TP = tp;
	}
	public Integer getTD() {
		return TD;
	}
	public void setTD(Integer td) {
		TD = td;
	}
	public Integer getBE() {
		return BE;
	}
	public void setBE(Integer be) {
		BE = be;
	}
	public Integer getControle() {
		return controle;
	}
	public void setControle(Integer controle) {
		this.controle = controle;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public Integer getYear() {
		return year;
	}
	public void setYear(Integer year) {
		this.year = year;
	}

}
